<?php 
/*rdo*/
header("Content-type: text/html; charset=utf-8;"); 
require '../ppf/fun.php';
require '../ppf/pdo_mysql.php';

session_start();
$pd=new pdo_mysql();

switch($_POST["tpl"]){
    case "getDct":
        $sl="select * from sys_dict where flag=0 and pid=(select id from sys_dict where name='".$_POST["name"]."') order by odx";
        getRst($sl);
		break;
    case "getSchool":
		$s_where="";
		if(!ckAdm()){
			$s_where=" and id=".$_SESSION["school"]." ";
		}
        $sl="select id,sch_name from sys_school where flag=0 ".$s_where;
        getRst($sl);
		break;
    case "get_dept":
		$s_where="";
		if(!ckAdm()){
			$s_where=" and mech=".$_SESSION["school"]." ";
		}
        $sl="select * from sys_department where flag=0 ".$s_where;
        getRst($sl);
        break;
    case "get_role": //角色
		$s_where="";
		if(!ckAdm()){
			$s_where=" and mech=".$_SESSION["school"]." ";
		}
        $sl="select * from sys_role where flag=0 ".$s_where;
        getRst($sl);
        break;
    case "get_dict_list":#获取地区
        $rs=$pd->query("select * from sys_area order by pidlist,lvl");
        $f='';
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			foreach($rs as $v){
				$f='';
				if($v['lvl']>1){
					for($i=0;$i<$v['lvl']-1;$i++){
						$f.='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
					}
				}
				$childs=getCidByPid($v['id'],$rs);
				$childs=($childs)?$v['id'].','.implode(",",$childs):$v['id'];//获取所有子级元素的id,用于搜索
				
				$disabled='';
				//if($v['childnums']>0)$disabled='disabled=""';//若有子级元素，则本身不可选
				$html.='<option value='.$v['id'].' childs="'.$childs.'" '.$disabled.' >'.$f.$v['areaname'].'</option>';
			}
		}
		echo $html;
		break;
	case "get_period_list":#获取学段
        $rs=$pd->query("select id,name from sys_dict where pid=14 order by id");
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			foreach($rs as $v){
				$html.='<option value='.$v['id'].'>'.$v['name'].'</option>';
			}
		}
		if(isset($_POST['period_arr']))$html.='&&'.json_encode($rs);
		echo $html;
		break;
	case "get_period_list2":#获取学段2
        $rs=$pd->query("select id,name from sys_dict where pid=14 order by id");
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			$html.=json_encode($rs);
		}
		echo $html;
		break;
	case "get_period_list3":#获取学段3
        $rs=$pd->query("select id,name from sys_dict where pid=14 order by id");
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			foreach($rs as $v){
				$html.='<input class="period_list" period='.$v['id'].' type="checkbox" name="period['.$v['id'].']" title="'.$v['name'].'">';
			}
		}
		echo $html;
		break;
	case "get_mechanism_list":#获取机构
        $rs=$pd->query("select id,name from sys_dict where pid=5 order by id");
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			foreach($rs as $v){
				$html.='<option value='.$v['id'].'>'.$v['name'].'</option>';
			}
		}
		echo $html;
		break;
	case "get_mtype_list":#获取机构类型
        $rs=$pd->query("select id,name from sys_dict where pidlist like '0,5%' and lvl=3 order by id");
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			foreach($rs as $v){
				$html.='<option value='.$v['id'].'>'.$v['name'].'</option>';
			}
		}
		echo $html;
		break;
	case "get_superior_list":#获取教育局
        $rs=$pd->query("select id,sch_name from sys_school where flag=0 and ischeck=0 and mechanism in (select id from sys_dict where name='教育局')");
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			foreach($rs as $v){
				$html.='<option value='.$v['id'].'>'.$v['sch_name'].'</option>';
			}
		}
		echo $html;
		break;
	case "get_user_byname":
		$e = $pd->fetchOneNo(array('field'=>'count(*)','table'=>'sys_member','where'=>"username='".$_POST['uname']."'"));
		if($e!=0){
			echo "ok";
		}
		else
		{
			echo "id";
		}
		break;
	#获取日志类型
	case "get_logtype":
        $rs=$pd->query("select id,name from sys_dict where pid=47 order by id");
		$html='';
		if($rs=$rs->fetchAll(PDO::FETCH_ASSOC)){
			foreach($rs as $v){
				$html.='<option value='.$v['id'].'>'.$v['name'].'</option>';
			}
		}
		echo $html;
		//exit;
		break;
	case "check_pass":
		$ps=$_POST['oldpass'];
		$user=$pd->query("select * from sys_member where id='".$_POST['id']."'")->fetch(PDO::FETCH_ASSOC);
		if($user){
			$o_pass=md5($ps);
			if($o_pass==$user["pmd5"]){
				echo "ok";
			}else{
				echo "no";
			}
		}
		break;
	case "update_pass":
		$ps=$_POST['npass'];
		$pd->exec("update sys_member set pmd5='".md5($ps)."' where id='".$_POST['id']."'");
		echo "ok";
		break;
	case "get_left_menu":
		$rid=$_SESSION["role"];
		$r_sql="";
		$html='';
		if(ckAdm()){
			$one_menu=$pd->query("select * from sys_menu where pid=".$_POST['pid']." and flag=0  order by odx asc");
			if($one_menu=$one_menu->fetchAll(PDO::FETCH_ASSOC)){
				foreach($one_menu as $v){
					$html.='<li class="layui-nav-item">';
					$html.='<a href="javascript:;"><i class="fa '.$v['ico'].'"></i>'.$v['name'].'</a>';
					$html.='<dl class="layui-nav-child">';
					$two_menu=$pd->query("select * from sys_menu where pid=".$v['id']." and flag=0  order by odx asc");
					if($two_menu=$two_menu->fetchAll(PDO::FETCH_ASSOC)){
						foreach($two_menu as $z){
							$html.='    <dd><a href="javascript:void(0);" data-url="'.$z['urls'].'" data-id="'.$z['id'].'">'.$z['name'].'</a></dd>';
						}
					}
					$html.='</dl>';
					$html.='</li>';
				}
			}
		}else{
			$menuid=$pd->query("select menu_id from sys_role_menu where rid=".$rid)->fetch(PDO::FETCH_ASSOC);
			$one_menu=$pd->query("select * from sys_menu where pid=".$_POST['pid']." and id in (".$menuid['menu_id'].")  order by odx asc");
			if($one_menu=$one_menu->fetchAll(PDO::FETCH_ASSOC)){
				foreach($one_menu as $v){
					$html.='<li class="layui-nav-item">';
					$html.='<a href="javascript:;"><i class="fa '.$v['ico'].'"></i>'.$v['name'].'</a>';
					$html.='<dl class="layui-nav-child">';
					$two_menu=$pd->query("select * from sys_menu where pid=".$v['id']." and id in (".$menuid['menu_id'].")  order by odx asc");
					if($two_menu=$two_menu->fetchAll(PDO::FETCH_ASSOC)){
						foreach($two_menu as $z){
							$html.='    <dd><a href="javascript:void(0);" data-url="'.$z['urls'].'" data-id="'.$z['id'].'">'.$z['name'].'</a></dd>';
						}
					}
					$html.='</dl>';
					$html.='</li>';
				}
			}
		}
		
		echo $html;
		break;
	case "get_schedule_byuid":
		$start = $_POST['start'];
		$end = strtotime($_POST['end'])+86400;
		$end = date('Y-m-d',$end);
		$schedule=$pd->query("select *,start as start_time,end as end_time from sys_schedule where end>='".$start."' and start<'".$end."' and uid='".$_POST['uid']."'")->fetchAll(PDO::FETCH_ASSOC);
		echo json_encode($schedule);
		break;
	case "get_ck_menu":
		$html="";
		$one_menu=$pd->query("select * from sys_menu where pid=0 and flag=0 and childnums>0 order by odx");
		if($one_menu=$one_menu->fetchAll(PDO::FETCH_ASSOC)){
			foreach($one_menu as $v){
				$html.= " <td valign=\"top\" class=\"auDiv\"><table id=\"Tb_Authority" . $v['id'] ."\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\" border=\"0\" class=\"dataTable auTable\">";
				$html.= "<tr class=\"dataTableHead\">";
				$html.="<td align=\"Left\" style=\"height: 24px;background-image: none;\">";
				$html.="<input type=\"checkbox\" name=\"mu\" value=\"" . $v['id'] ."\" title=\"".$v['name']."\" lay-filter=\"one_menu\">";
				$html.="</td>";
				$html.="</tr>";
				//22
				$two_menu=$pd->query("select * from sys_menu where pid=" . $v['id'] ." and flag=0 order by odx");
				if($two_menu=$two_menu->fetchAll(PDO::FETCH_ASSOC)){
					foreach($two_menu as $w){
						$html.="<tr class=\"tdbg\" style=\"height: 25px;\">";
						$html.="<td align=\"Left\">";
						$html.= "<hr />";
						$html.="<img src='/res/images/easyicon_cn_16.png'/>";
						$html.="--<input type=\"checkbox\" name=\"mu\" value=\"" . $w['id'] ."\" data-id=\"" . $v['id'] ."\" title=\"".$w['name']."\" lay-filter=\"two_menu\">";
						$html.= "</td>";
						$html.="</tr>";
						//33
						$menu3=$pd->query("select * from sys_menu where pid=" . $w['id'] ." and flag=0 order by odx");
						if($menu3=$menu3->fetchAll(PDO::FETCH_ASSOC)){
							foreach($menu3 as $x){
								$html.="<tr class=\"tdbg\" style=\"height: 25px;\">";
								$html.="<td align=\"Left\">";
								$html.= "<hr />";
								$html.="<img src='/res/images/cn_bg_012.png'/>";
								$html.=" ----<input type=\"checkbox\" name=\"mu\" value=\"" . $x['id'] ."\" data-id=\"" . $v['id'] ."\" data-id1=\"" . $w['id'] ."\" title=\"".$x['name']."\" lay-filter=\"check_cld\">";
								$html.= "</td>";
								$html.="</tr>";
								//44
								$menu4=$pd->query("select * from sys_menu where pid=" . $x['id'] ." and flag=0 order by odx");
								if($menu4=$menu4->fetchAll(PDO::FETCH_ASSOC)){
									foreach($menu4 as $y){
										$html.="<tr class=\"tdbg\" style=\"height: 25px;\">";
										$html.="<td align=\"Left\">";
										$html.= "<hr />";
										$html.="<img src='/res/images/cn_bg_011.png'/>";
										$html.=" ------<input type=\"checkbox\" name=\"mu\" value=\"" . $y['id'] ."\" title=\"".$y['name']."\" lay-filter=\"check_cld\">";
										$html.= "</td>";
										$html.="</tr>";
									}
								}
							}
						}
						$html.="";
					}
				}


				$html.= "</table></td>";
			}
		}
		echo $html;
		break;
	case "getTakesUser":
		$sl="select * from sys_member order by id desc";
		getRst($sl);

		break;
	case "get_repair_userlist":
		$repair_id = trim(intval($_POST["repair_type"]));
        $html = "";
		if(empty($repair_id)){
            $html = "没有数据";
		}else{
			$sql = "select repair_user_id,repair_user_name from sys_equipment_repair where id='{$repair_id}'";
			$list = $pd->query($sql)->fetchAll(PDO::FETCH_ASSOC);
            $html = "<option value=''>--请选择维修人员--</option>";
            $user_arr = explode(',',$list[0]['repair_user_name']);
            $user_arr_id = explode(',',$list[0]['repair_user_id']);
            $len = count($user_arr);
            for($i=0;$i<$len;$i++){
                $html.="<option value='".$user_arr_id[$i]."'>".$user_arr[$i]."</option>";
            }
		}
		echo $html;
		break;

}
    /*获取指定栏目id下子栏目（默认所有级）信息,返回其组成的数组,可指定返回前xx级的子栏目
	 * id必须，type和flag可选，level固定为0，无需指定
	 * type不指定或为空时，仅仅返回由子栏目id组成的数组
	 * type=1时，返回子栏目相关信息的二维数组，并新增层级属性level
	 * flag表示返回前xx级的所有子栏目，默认-1，返回所有级,flag=1时表示返回第一级子栏目
	 */
	function getCidByPid($id,$rs,$type='',$flag='-1',$level=0){
		static $arr = array(); //使用static
		if($level==0)$arr=array();//第一次调用此函数时，先重置静态变量$arr,否则会有累加效果
		if($level==$flag)return $arr;
	    $level++;
	    foreach($rs as $key => $value)
	    {
	        if($value['pid' ] == $id)
	        {
	            $value[ 'level'] = $level;
				if($type==''){
					$arr[] = $value['id'];
				}elseif($type=='1'){
					$arr[] = $value;
				}else{
					return false;
				}
	            getCidByPid($value['id'],$rs,$type,$flag,$level);
	        }
	    }
	    return $arr;
	}
function getRst($sql){
    try{
		$pbd=new pdo_mysql(); 
		$rs=$pbd->query($sql); 
		$json_result=json_encode($rs->fetchAll(PDO::FETCH_ASSOC),JSON_UNESCAPED_UNICODE); 
        echo $json_result;
		$pbd->close(); 
		unset($pbd); 
		unset($rs);
	}catch(Exception $e)
	{
		echo 'Message: ' .$e->getMessage();
	}
}
$pd->close();
unset($pd);
unset($res);